﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TracNghiem.Areas.Admin.Models;
using System.Web.Helpers;
using System.IO;

namespace TracNghiem.Areas.Admin.Controllers
{
    [Authorize(Roles = "admin")]
    public class Bai_VietController : Controller
    {
        //
        // GET: /Admin/Bai_Viet/



        Bai_VietModels baihocmodels = new Bai_VietModels();
        Noi_DungModels noidungmodels = new Noi_DungModels();
        DichvuModel DichvuModel = new DichvuModel();
        public ActionResult Index()
        {
            List<Bai_VietModels> ts = baihocmodels.getbaihoc();

            ViewBag.theanh = Get_select();
            List<DichvuModel> dichvuModel = DichvuModel.getDichvu();
            ViewBag.dichvu_id = new SelectList(dichvuModel, "id", "name");
            //selectedDichVu_id
            return View(ts);
        }

        public ActionResult AddBaiHoc()
        {
            //List<DichvuModel> dichvuModel = DichvuModel.getDichvu();
            //baihocmodels.service_id = new SelectList(dichvuModel, "id", "name");

            ViewBag.theanh = Get_select();
            return View(baihocmodels);
        }
        [HttpPost]
        [ValidateInput(false)]
        public ActionResult AddBaiHoc(Bai_VietModels bh)
        {
            var image = WebImage.GetImageFromRequest();
            if (image != null)
            {
                if (image.Width > 100)
                {
                    image.Resize(100, 120);

                }
                var filename = Path.GetFileName(image.FileName);


                var path = Path.Combine(Server.MapPath("~/Content/themes/anh"), filename);
                image.Save(path); //Lưu ảnh trên server
                bh.anh = filename;


             
            }
            bh.anh = "the.png";
            baihocmodels.addbaihoc(bh);
            TempData["msg"] = "Thông tin mới đã được thêm mới thành công!";
            return RedirectToAction("Index");

            TempData["msg"] = "Bạn đã nhập sai thông tin!";
            return View(bh);

            //return RedirectToAction("Index");
        }
        public ActionResult DeleteBaiHoc(int id)
        {

            baihocmodels.deleteBaiHoc(id);

            TempData["msg"] = "Thông tin mới đã được cập nhật thành công!";
            return RedirectToAction("Index");
        }

        public ActionResult Editbaihoc(int id)
        {
            Bai_VietModels _baihocmodels = baihocmodels.getbaihoc().Find(delegate(Bai_VietModels s) { return s.id == id.ToString(); });
            string A = _baihocmodels.dichvu_id;
            List<DichvuModel> dichvuModel = DichvuModel.getDichvu();
            _baihocmodels.service_id = new SelectList(dichvuModel, "id", "name");
            ViewBag.theanh = Get_select();
            return View(_baihocmodels);
        }
        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Editbaihoc(Bai_VietModels bh)
        {
            string anh_ = Request.Form["anh_"];
            var image = WebImage.GetImageFromRequest();
            if (image != null)
            {
                if (image.Width > 100)
                {
                    image.Resize(100, 120);

                }
                var filename = Path.GetFileName(image.FileName);


                var path = Path.Combine(Server.MapPath("~/Content/themes/anh"), filename);
                image.Save(path); //Lưu ảnh trên server
                bh.anh = filename;

                baihocmodels.updateBaihoc(bh);
                TempData["msg"] = "Thông tin mới đã được thêm mới thành công!";
                return RedirectToAction("Index");
            }
            else
            {
                bh.anh = anh_;
            }
            baihocmodels.updateBaihoc(bh);
            TempData["msg"] = "Thông tin mới đã được thêm mới thành công!";
            return RedirectToAction("Index");
           // TempData["msg"] = "Bạn đã nhập sai thông tin!";
            // List<DichvuModel> dichvuModel = DichvuModel.getDichvu();
            // baihocmodels.service_id = new SelectList(dichvuModel, "id", "name");
            return View(bh);
        }



        public ActionResult FindTest_Subject()
        {
            return RedirectToAction("Index");
        }
        [HttpPost]
        [ValidateInput(false)]
        public ActionResult FindTest_Subject(string timkiem)
        {
            string dichvu_id = "";
            int num;
            bool dichvu_id_check = int.TryParse(dichvu_id, out num);
            string s = "";
            //add by Namtd 09/03/2014
            ViewBag.selectedDichvu = "";
            if (int.TryParse(Request.Form["service_id"], out num))
            {
                dichvu_id = Request.Form["service_id"];
                s = " b.id=" + dichvu_id + " AND ";
                ViewBag.selectedDichvu = Request.Form["service_id"];
            }
            string sql;
            string dk = "";
            if (Request["select_option"] == "chu_de.name")
                dk = "a.name";
            //else if (Request["select_option"] == "stations.name")
            //    dk = "b.name";
            //else if (Request["select_option"] == "main_stations.name")
            //    dk = "d.name";            
            if (timkiem == "")
                sql = s + dk + " like '%" + timkiem + "%' or " + dk + " is null";
            else
                sql = s + "(" + dk + " like '%" + timkiem + "%' or upper(" + dk + ") like upper('%" + timkiem + "%'))";

            ViewBag.find_textbox = timkiem;
            ViewBag.find_selectbox = Request["select_option"];
            Test_SubjectModel Test_SubjectModel = new Test_SubjectModel();
            List<Test_SubjectModel> Test_Subject = Test_SubjectModel.FindTest_Subject(sql);
            List<DichvuModel> dichvuModel = DichvuModel.getDichvu();
            if (ViewBag.selectedDichvu.Length == 0)
                ViewBag.dichvu_id = new SelectList(dichvuModel, "id", "name");
            else
            {
                ViewBag.dichvu_id = new SelectList(dichvuModel, "id", "name", ViewBag.selectedDichvu);
            }
            return View("Index", Test_Subject);
        }
        public static string Get_select()
        {
            DichvuModel dv = new DichvuModel();
            List<DichvuModel> dichvuModel = dv.getDichvu_byid(0,0);
            string kq = "";
            foreach (var item in dichvuModel)
            {
                kq += "<option value='"+item.id+"'>"+item.name+"</option>";
                List<DichvuModel> dvc = dv.getDichvu_byid(int.Parse(item.id),0);
                foreach (var con in dvc)
                {
                    kq += "<option value='" + con.id + "'>-->" + con.name + "</option>";
                }
            }

            return kq;

        }
        [HttpPost]
        public ActionResult FindBai_Viet(string timkiem)
        {
            //int id = int.Parse(Request.Form["dichvu_id"]);
           // int id = int.Parse(Request.Form["dichvu_id"]);
            string sql = "";
            string s = "";
            int num;
             if (int.TryParse(Request.Form["dichvu_id"], out num))
            {
                s = " b.id=" + Request.Form["dichvu_id"] + " AND ";
                ViewBag.selectedDichVu_id = Request.Form["option_id"];
            }
            if (timkiem == "")
                sql = s + "a.name" + " like '%" + timkiem + "%' or " + "a.name" + " is null";
            else
                sql = s + "(" + "a.name" + " like '%" + timkiem + "%' or upper(" + "a.name" + ") like upper('%" + timkiem + "%'))";


            List<Bai_VietModels> ts = baihocmodels.FindBai_Viet(sql);

            ViewBag.theanh = TracNghiem.Models.General.Get_select(Request.Form["dichvu_id"]);
            List<DichvuModel> dichvuModel = DichvuModel.getDichvu();
            ViewBag.dichvu_id = new SelectList(dichvuModel, "id", "name");
            ViewBag.timkiem = timkiem;

            return View("index",ts);



           
            //Test_QuestionModel Test_QuestionModel = new Test_QuestionModel();
            //List<Test_QuestionModel> Test_Question = Test_QuestionModel.FindTest_Question(id, sql);
            //List<PhanLoaiModel> PhanLoaiModel_ = PhanLoaiModel.getPhan_LoaiByDV(id);
            //if (ViewBag.selectedPhanloai.Length == 0)
            //    ViewBag.phanloai_id = new SelectList(PhanLoaiModel_, "id", "name");
            //else
            //{
            //    ViewBag.phanloai_id = new SelectList(PhanLoaiModel_, "id", "name", ViewBag.selectedPhanloai);
            //}

            //DichvuModel DichvuModel_ = DichvuModel.getDichvu().Find(delegate(DichvuModel dv) { return dv.id == id.ToString(); });
            //ViewBag.dichvu_id = id;
            //ViewBag.ten_dich_vu = DichvuModel_.name;
            //return View("Index", Test_Question);
        }
    }
}
